﻿Imports Microsoft.Practices.Composite.Wpf.Regions
Imports Microsoft.Practices.Composite.Regions
Imports Infragistics.Windows.Ribbon
Imports System.Collections.Specialized
Imports System.ComponentModel

Namespace RegionAdapters

    Public Class RibbonRegionAdapter
        Inherits RegionAdapterBase(Of XamRibbon)

        Private m_regionTarget As XamRibbon

        Protected Overrides Sub Adapt(ByVal region As Microsoft.Practices.Composite.Regions.IRegion, ByVal regionTarget As XamRibbon)
            m_regionTarget = regionTarget

            'If control has child items, move them to the region and then bind control to region. Can't set ItemsSource if child items exist.

            ' Process an clear Tabs
            If regionTarget.Tabs.Count > 0 Then
                For Each childItem In regionTarget.Tabs
                    region.Add(childItem)
                Next
                regionTarget.Tabs.Clear()
            End If

            ' process and clear Contextual Tabs
            If regionTarget.ContextualTabGroups.Count > 0 Then
                For Each childItem In regionTarget.ContextualTabGroups
                    region.Add(childItem)
                Next
                regionTarget.ContextualTabGroups.Clear()
            End If

            AddHandler region.ActiveViews.CollectionChanged, AddressOf OnActiveViewsChanged

            ' Add Tabs
            For Each v In region.ActiveViews
                If v.GetType Is GetType(RibbonTabItem) Then
                    regionTarget.Tabs.Add(v)
                ElseIf v.GetType Is GetType(ContextualTabGroup) Then
                    regionTarget.ContextualTabGroups.Add(v)
                Else
                    Throw New InvalidCastException("Views must be of either RibbonTabItem or ContextualTabGroup")
                End If

            Next


        End Sub

        Private Sub OnActiveViewsChanged(ByVal sender As Object, ByVal e As NotifyCollectionChangedEventArgs)
            Select Case e.Action
                Case NotifyCollectionChangedAction.Add
                    For Each v In e.NewItems
                        If v.GetType Is GetType(RibbonTabItem) Then
                            m_regionTarget.Tabs.Add(v)
                        ElseIf v.GetType Is GetType(ContextualTabGroup) Then
                            m_regionTarget.ContextualTabGroups.Add(v)
                        Else
                            Throw New InvalidCastException("Views must be of either RibbonTabItem or ContextualTabGroup")
                        End If
                    Next
                Case NotifyCollectionChangedAction.Remove
                    For Each v In e.OldItems
                        If v.GetType Is GetType(RibbonTabItem) Then
                            m_regionTarget.Tabs.Remove(v)
                        ElseIf v.GetType Is GetType(ContextualTabGroup) Then
                            m_regionTarget.ContextualTabGroups.Remove(v)
                        Else
                            Throw New InvalidCastException("Views must be of either RibbonTabItem or ContextualTabGroup")
                        End If
                    Next
            End Select
        End Sub

        Protected Overrides Function CreateRegion() As Microsoft.Practices.Composite.Regions.IRegion
            Return New AllActiveRegion
        End Function

    End Class

End Namespace